#pragma once

#include "AsyncLibDef.h"

namespace AsyncLib
{

class CThreadpoolTimer
{
public:
	CThreadpoolTimer();
	~CThreadpoolTimer();

	void Init();
	void Stop();
	void Exit();
	void AddTask(UINT nMilliseconds, std::function<void()>&& fnTask);

private:
	void ThreadProc();

	struct TimeTask
	{
		std::chrono::system_clock::time_point dueTime;
		std::function<void()> fnTask;

		bool operator<(const TimeTask& task) const
		{
			return dueTime > task.dueTime;
		}
	};

	std::mutex m_mutex;
	std::condition_variable m_condVar;
	std::priority_queue<TimeTask> m_queTasks;
	pthread_t m_threadId;
};

} // namespace AsyncLib
